package com.yuhanginfo.modules.web.web;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yuhanginfo.common.web.BaseController;
import com.yuhanginfo.modules.web.entity.CarVinDic;
import com.yuhanginfo.modules.web.entity.LLQCar;
import com.yuhanginfo.modules.web.entity.WebCar;
import com.yuhanginfo.modules.web.service.CarVinDicService;
import com.yuhanginfo.modules.web.service.GoodsListService;
import com.yuhanginfo.modules.web.service.WebCarService;
import com.yuhanginfo.modules.web.service.WebClassService;
import com.yuhanginfo.modules.web.util.CarInfo;

/**
 * 配件目录
 *
 */
@Controller
@RequestMapping(value = "${frontPath}/partList")
public class PartListController extends BaseController{
	
	@Autowired
	private GoodsListService goodsListService;
	@Autowired
	private WebClassService webClassService;
	@Autowired
	private CarVinDicService carVinDicService;
	@Autowired
	private WebCarService webCarService;
	
	@RequestMapping(value = {"", ""})
	public String list(HttpServletRequest request, HttpServletResponse response, Model model) throws Exception {
		//查询所有品牌
		model.addAttribute("carBarandList", CarInfo.getCarBarandInfo());
		model.addAttribute("allClassesList",webClassService.getAllClasses());
		//封装5层数据
		List<WebCar> carInfo=goodsListService.getCarInfo();
		model.addAttribute("carInfo", carInfo);
		return "modules/web/partList";
	}
	
	@ResponseBody
	@RequestMapping(value = "getCarInfoByVin")
	public LLQCar getCarInfoByVin(String vin){
		LLQCar car=CarInfo.getCarInfoByVin("all", vin);
		if(car.getMains()!=null){
			List<WebCar> carList = getMatchingCar(car);
			car.setCarList(carList);
		}
		return car;
	}

	private List<WebCar> getMatchingCar(LLQCar car) {
		List<CarVinDic> carVinDics = carVinDicService.getAllGroupByCarId();
		List<Integer> carIdList = new ArrayList<Integer>();
		//一个carId对应多个关键词
		outer:for (CarVinDic carVinDic : carVinDics) {
			//判断该carId是否满足
			boolean flag = false;
			//获取一个carId对应的所有关键词
			List<String> names = carVinDic.getNames();
			//遍历多个关键词
			for (String name : names) {
				if("".equals(name)){
					flag = false;
				}else{
					String[] split = name.split("/");
					//对每个关键词进行判断是否存在于main中
					//如果返回true，则检验下一个关键词，如果返回false，直接break
					flag = isIndexOf(split,car.getMains().substring(0, 100));
				}
				//如果有关键词不满足，直接break
				if(!flag){
					continue outer;
				}
			}
			//如果都满足，保存该carId
			if(flag){
				carIdList.add(carVinDic.getCarId());
			}
		}
		List<WebCar> carListByIds = new ArrayList<WebCar>();
		if(!carIdList.isEmpty()){
			carListByIds = webCarService.getCarListByIds(carIdList);
		}
		return carListByIds;
	}
	
	private boolean isIndexOf(String[] split, String mains) {
		for (String string : split) {
			if(mains.contains(string)){
				return true;
			}
		}
		return false;
	}
}
